(function ($) {
	var pageType=''
	var statArr = []
	var methodName = '',tipType="",nurseid='',perId=''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getStatData(){
		//perId:为空则统计权限范围内所有人员  year  nurseid  appFlag:1  appRet:""
		var year = $('#statDate')[0].innerText.substring(0,4)
		var pdict = {'perId':perId,'year':year,'nurseid':nurseid,'appFlag':'1','appRet':''}
		var methodType = 'MethodNoFilter'
		if (pageType == '调配申请') {
			pdict = {'parr':year+'-01-01^'+year+'-12-31^month','nurseid':nurseid}
			methodType = 'RecQuery'
		}
		var paramDict = {'method':methodName,'params':JSON.stringify(pdict),'type':methodType}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				showNullTip(result.msg)
				return
			}
			if (pageType == '调配申请') {
				updateTPUI(result['data'])
			}else{
				updateUI(result['data']);
			}
		}, function(errorStr) {
			showNullTip(errorStr)
		});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		pageType = self.pageType
		nurseid = self.nurseid
		perId = self.perId||''
		$('#typeTitle')[0].innerHTML = self.title
		getStatPageData();
		//统计信息的关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			mui.back()
		})
		//统计
		var dateObj = new Date();
		$('#statDate')[0].innerHTML =  dateObj.getFullYear() + '年';
		mui('body').on('tap','#statDate',selectYear)
		getStatData()
	}
	function updateUI(dataDict){
		var count = 0
		var innerHtml = ''
		for (var i = 0; i < statArr.length;i++) {
			var oneData = statArr[i];
			var cellArr = dataDict[oneData['key']]
			if (!cellArr) {
				continue
			}
			var oneCount = 0
			var html = '<div class="areaTitle">'+oneData['title']+'</div>'
			for (var j = 0; j < cellArr.length;j++) {
				var oneCell = cellArr[j]
				var num = oneCell['Num']
				html +='<div class="areaCard">'+
					'<div class="cardTitle mui-ellipsis">'+oneCell['Desc']+'</div><div class="cardNum">'+num+'</div>'+
					'</div>'
				if (parseInt(num)) {
					oneCount += parseInt(num)
				}
			}
			html +='</div>';
			innerHtml += '<li class="mui-table-view-cell">'+html+'</li>'
			if (count < oneCount) {
				count = oneCount
			}
		}
		$('#UL')[0].innerHTML = innerHtml;
		$('#typeNum')[0].innerHTML = oneCount + tipType;
		if (innerHtml == '') {
			showNullTip('暂无数据')
		}else{
			hideNullTip()
		}
	}
	
	//调配申请的统计
	function updateTPUI(dataArr){
		var count = 0
		var innerHtml = ''
		var month =  new Date().getMonth() + 1;
		for (var i = 0; i < dataArr.length;i++) {
			var oneDict = dataArr[i]
			var oneCount = parseInt(oneDict['Summary'])
			count += oneCount
			var html = '<div class="areaTitle">'+oneDict['Ward']+'</div>'
			for (j = 1;j < 13;j++) {
				var desc = j;
				if (j < 10) {
					desc = '0'+j
				}
				var num = '--'
				if (j <= month) {
					num = oneDict[desc]==undefined?'0':oneDict[desc]
				}
				html +='<div class="areaCard">'+
					'<div class="cardTitle mui-ellipsis">'+desc+'月</div><div class="cardNum">'+num+'</div>'+
					'</div>'
			}
			html +='</div>';
			innerHtml += '<li class="mui-table-view-cell">'+html+'</li>'
		}
		$('#UL')[0].innerHTML = innerHtml;
		$('#typeNum')[0].innerHTML = count + tipType;
		if (innerHtml == '') {
			showNullTip('暂无数据')
		}else{
			hideNullTip()
		}
	}
	
	function selectYear() {
		var _self = this
		if(_self.picker) {
			_self.picker.show(function(rs) {
				_self.innerText = rs.text.substring(0,4)+'年'
				getStatData();
				_self.picker.dispose();
				_self.picker = null;
			});
		} else {
			var optionsJson = '{"type":"month"}';
			var options = JSON.parse(optionsJson);
			_self.picker = new $.DtPicker(options);
			_self.picker.show(function(rs) {
				_self.innerText = rs.text.substring(0,4)+'年'
				getStatData();
				_self.picker.dispose();
				_self.picker = null;
			});
		}
	}
	function getStatPageData(){
		//统计页面数据
		tipType = '个'
		switch (pageType){
			//科研管理
			case '发表论文备案':
				methodName = 'web.INMSMComm:GetThesisSta'
				statArr = [{'title':'是否本专科','key':'Spe'},{'title':'期刊类型','key':'Level'},{'title':'论文类型','key':'Type'}]
				tipType = '篇'
				break;
			case '获奖论文备案':
				methodName = 'web.INMSMComm:GetAwardSta'
				statArr = [{'title':'会议级别','key':'Level'},{'title':'获得奖项','key':'Type'},{'title':'是否本专科','key':'Spe'}]
				tipType = '篇'
				break;
			case '上交论文备案':
				methodName = 'web.INMSMComm:GetEndYearSta'
				statArr = [{'title':'论文类型','key':'Type'}]
				tipType = '篇'
				break;
			case '科研立项备案':
				methodName = 'web.INMSMComm:GetRegScience'
				statArr = [{'title':'科研类型','key':'Type'}]
				break;
			case '科室创新备案':
				methodName = 'web.INMSMComm:GetLocInnovateSta'
				statArr = [{'title':'创新类型','key':"Type"},
					{'title':'是否本专科','key':'Spe'}]
				break;
			case '获奖创新备案':
				methodName = 'web.INMSMComm:GetAwaInnovate'
				statArr = [{'title':'创新类型','key':'Type'},
					{'title':'获得奖项','key':'Score'},
					{'title':'奖项级别','key':'Level'},
					{'title':'是否本专科','key':'Spe'}]
				break;
			case '专利备案':
				methodName = 'web.INMSMComm:GetPatentSta'
				statArr = [{'title':'专利类型','key':'Type'}]
				break;
				
			//专科管理
			case '专科活动备案':
				methodName = 'web.INMSpecialComm:GetActivitySta'
				statArr = [{'title':'活动形式','key':'Type'},{'title':'参与形式','key':'ParType'},{'title':'级别','key':'Level'}]
				break;
			case '课题专案备案':
				methodName = 'web.INMSpecialComm:GetCaseSta'
				statArr = [{'title':'课题/专案','key':'Type'},{'title':'参与形式','key':'ParType'},{'title':'级别','key':'Level'}]
				break;
			case '规范参与备案':
				methodName = 'web.INMSpecialComm:GetStandardSta'
				statArr = [{'title':'类型','key':'Type'},{'title':'制定/修订','key':'TypeSub'},{'title':'专业组','key':'Group'}]
				break;
				
			//个人管理
			case '调配申请':
				tipType = '人次'
				methodName = 'web.INMPersonCountComm:FindTransCountData'
				statArr = []
				break;
			case '投诉备案':
				methodName = 'web.INMSpecialComm:GetCaseSta'
				statArr = [{'title':'课题/专案','key':'Type'},{'title':'参与形式','key':'ParType'},{'title':'级别','key':'Level'}]
				break;
			case '表扬备案':
				methodName = 'web.INMSpecialComm:GetStandardSta'
				statArr = [{'title':'类型','key':'Type'},{'title':'制定/修订','key':'TypeSub'},{'title':'专业组','key':'Group'}]
				break;
			default:
				break;
		}
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		mui('.nullData')[0].style.display = 'none';
	}
})(mui)